package com.cennavi.minenavidemo.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.utils.OrientationEventListener;
import com.cennavi.minenavidemo.utils.ScreenUtil;
import com.minedata.minenavi.mapdal.DatastoreItem;

import java.util.ArrayList;
import java.util.List;


public class SelectItemDialog extends PopupWindow implements OrientationEventListener.OrientationChangeListener {
    private static final String TAG = "SelectItemDialog";

    private Context mContext;
    private View mSelectDialogContentView; // 当前dialog显示的内容
    private int navigationBarHeight;
    private View mMessageCenter;
    private RecyclerView mMessageCenterrecyclerView;
    private int rotation;
    private boolean changed = true;
    private ScreenUtil mScreenUtil;

    public interface selectState {
        void getState();
    }

    public interface ItemCheckedInterface {
        void itemChecked(String s);
    }

    public interface MessageCenterInterface {
        void itemChecked();
    }

    ItemCheckedInterface mItemCheckedInterface;
    MessageCenterInterface mMessageCenterInterface;

    public void checkedPosition(ItemCheckedInterface itemCheckedInterface) {
        this.mItemCheckedInterface = itemCheckedInterface;
    }

    public void seeDetail(MessageCenterInterface messageCenterInterface) {
        this.mMessageCenterInterface = messageCenterInterface;
    }

    public View mMenuView;
    private int mMessageCenterTopMargin;

    public SelectItemDialog(Context context) {
        super(context);
        mContext = context;
        OrientationEventListener orientationEventListener = new OrientationEventListener(mContext);
        orientationEventListener.setOrientationChangeListener(this);
        orientationEventListener.enable();
        navigationBarHeight = ScreenUtil.getInstance().getNavigationBarHeight();
        mScreenUtil = ScreenUtil.getInstance();
    }

    /**
     * 初始化数据商店查看下载信息的dialog
     */
    public void initDataStoreCheckDownloadDataSelectItemDialog(OnClickListener itemsOnClick, DatastoreItem item) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.data_stores_menu, null);
        this.mMenuView = view;
        TextView tvTopName = (TextView) view.findViewById(R.id.tv_top_name);
        TextView checkMap = (TextView) view.findViewById(R.id.check_map);
        View line1 = view.findViewById(R.id.line_1);
        TextView delete = (TextView) view.findViewById(R.id.delete);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        mSelectDialogContentView = view.findViewById(R.id.select_dialog_content_view);

        tvTopName.setText(item.name);
        if (item.name.equals("电子眼") || item.name.equals("基础数据") || item.hasSubnodes) {
            checkMap.setVisibility(View.GONE);
            line1.setVisibility(View.GONE);
        }

        checkMap.setOnClickListener(itemsOnClick);
        delete.setOnClickListener(itemsOnClick);
        cancel.setOnClickListener(itemsOnClick);

        setDialogAttribute(view);
        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mSelectDialogContentView.getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }


    public View getinfo() {
        return mMenuView;
    }

    private void setDialogAttribute(View menuView) {
        setClippingEnabled(false);
        this.setContentView(mMenuView);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setSplitTouchEnabled(false);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    private List<String> mHourList = new ArrayList<>();
    private List<String> mMinuteList = new ArrayList<>();
    private List<String> mCenterList = new ArrayList<>();
    private String mBusinessTimeState;
    private TextView mSelectEveryday, mSelectHoliday, mSelectWorkday;

    public interface OnFeedbackBusinessTimeSelectMessageListener {
        void onBusinessTime(String state, String startHour, String startMinute, String endHour, String endMinute);

        void onClearClick();
    }

    private View.OnTouchListener mOptionsOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mSelectDialogContentView instanceof ScrollView) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    ((ScrollView) mSelectDialogContentView).requestDisallowInterceptTouchEvent(false);
                } else {
                    ((ScrollView) mSelectDialogContentView).requestDisallowInterceptTouchEvent(true);
                }
            }
            return false;
        }
    };


    private void initFeedbackBusinessTimeData() {
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                mHourList.add("0" + i + "时");
            } else {
                mHourList.add(i + "时");
            }
        }

        for (int i = 0; i < 60; i++) {
            if (i % 5 == 0) {
                if (i == 0 || i == 5) {
                    mMinuteList.add("0" + i + "分");
                } else {
                    mMinuteList.add(i + "分");
                }
            }
        }

        mCenterList.add("至");
    }


    @Override
    public void dismiss() {
        super.dismiss();
    }

    public void showDialog() {
        updateSelectDialogShowPosition();
    }

    private void updateSelectDialogShowPosition() {
        if (((Activity) mContext).isFinishing() && !isShowing()) return;
        if (ScreenUtil.getInstance().horizontalDirection() == ScreenUtil.NAVIGATION_BAR_ON_RIGHT) {
            showAtLocation(((Activity) mContext).getWindow().getDecorView(), Gravity.RIGHT
                    , navigationBarHeight, 0);
            mMenuView.setPadding(navigationBarHeight, 0, 0, 0);
        } else if (ScreenUtil.getInstance().horizontalDirection() == ScreenUtil.NAVIGATION_BAR_ON_LiFT) {
            showAtLocation(((Activity) mContext).getWindow().getDecorView(), Gravity.LEFT
                    , navigationBarHeight, 0);
            mMenuView.setPadding(0, 0, navigationBarHeight, 0);
        } else {
            showAtLocation(((Activity) mContext).getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL
                    , 0, navigationBarHeight);
            mMenuView.setPadding(0, 0, 0, 0);
        }
    }

    @Override
    public void orientationChanged(int orientation) {
        if (changed) {
            rotation = ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation();
            changed = false;
        }
        if (rotation != ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation()) {
            if (isShowing()) {
                dismiss();
                updateSelectDialogShowPosition();
            }
            rotation = ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation();
            changed = true;
        }
    }

    private void setMessageCenterParams() {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mMessageCenterrecyclerView.getLayoutParams();
        if (ScreenUtil.getInstance().horizontalDirection() == ScreenUtil.getInstance().NAVIGATION_BAR_ON_RIGHT) {
            layoutParams.topMargin = mContext.getResources().getDimensionPixelSize(R.dimen.nz_px_58);
            layoutParams.leftMargin = ScreenUtil.getInstance().getHorizontalMaxWidth();
            layoutParams.rightMargin = 0;
            mMenuView.setPadding(navigationBarHeight, 0, 0, 0);
        } else if (ScreenUtil.getInstance().horizontalDirection() == ScreenUtil.getInstance().NAVIGATION_BAR_ON_LiFT) {
            layoutParams.topMargin = mContext.getResources().getDimensionPixelSize(R.dimen.nz_px_58);
            layoutParams.leftMargin = ScreenUtil.getInstance().getHorizontalMaxWidth();
            layoutParams.rightMargin = navigationBarHeight;
            mMenuView.setPadding(0, 0, 0, 0);
        } else {
            layoutParams.topMargin = mMessageCenterTopMargin;
            layoutParams.leftMargin = 0;
            layoutParams.rightMargin = 0;
            mMenuView.setPadding(0, 0, 0, 0);
        }
        mMessageCenterrecyclerView.setLayoutParams(layoutParams);
    }
}